import { ExamAnswerMode, ExamMatchRule, QuestionType, TemplateSchema, TemplateVO } from '@/api/exam/template/types';
import { generateRandomId } from '@/views/exam/template/info/config';
import { ProjectMode } from '@/api/exam/project/types';
import { ElDatePicker, ElInputNumber } from 'element-plus';
import { Component } from 'vue';

// region 项目编辑配置项

export const ADD_PROJECT = {
  [ProjectMode.Exam]: {
    name: '试卷标题',
    mode: ProjectMode.Exam,
    survey: {
      title: '试卷标题',
      children: [],
      attribute: {
        mode: ProjectMode.Exam,
        examScore: 0
      }
    },
    setting: {
      mode: ProjectMode.Exam,
      answerSetting: {
        progressBar: true,
        onePageOneQuestion: false,
        answerSheetVisible: true
      }
    }
  }
};

// 定义题型处理策略对象
export const projectQuestionTypeStrategies = {
  [QuestionType.Radio]: {
    icon: 'radio-hollow',
    inputType: 'text',
    modelPath: 'title',
    answerModes: [
      { label: '此问题有唯一答案', value: 'onlyOne' },
      { label: '每个选项有对应分值', value: 'select' },
      { label: '人工评分', value: 'manual' }
    ],
    showCorrectAnswer: (question, item) =>
      item.attribute.examCorrectAnswer === item.id &&
      ![ExamAnswerMode.select, ExamAnswerMode.selectCorrect].includes(question.attribute.examAnswerMode),
    showScore: (question, item) =>
      !!item.attribute.examCorrectAnswer &&
      [ExamAnswerMode.select, ExamAnswerMode.selectCorrect].includes(question.attribute.examAnswerMode)
  },
  [QuestionType.TrueFalse]: {
    icon: 'radio-hollow',
    inputType: 'text',
    modelPath: 'title',
    answerModes: [
      { label: '此问题有唯一答案', value: 'onlyOne' }
    ],
    showCorrectAnswer: (question, item) =>
      item.attribute.examCorrectAnswer === item.id &&
      ![ExamAnswerMode.select, ExamAnswerMode.selectCorrect].includes(question.attribute.examAnswerMode),
    showScore: (question, item) =>
      !!item.attribute.examCorrectAnswer &&
      [ExamAnswerMode.select, ExamAnswerMode.selectCorrect].includes(question.attribute.examAnswerMode)
  },
  [QuestionType.Checkbox]: {
    icon: 'checkbox-hollow',
    inputType: 'text',
    modelPath: 'title',
    answerModes: [
      { label: '全部答对才得分', value: 'selectAll' },
      { label: '答对几项得几分，答错不得分', value: 'selectCorrect' },
      { label: '每个选项有对应分值', value: 'select' },
      { label: '人工评分', value: 'manual' }
    ],
    showCorrectAnswer: (question, item) =>
      item.attribute.examCorrectAnswer === item.id &&
      ![ExamAnswerMode.select, ExamAnswerMode.selectCorrect].includes(question.attribute.examAnswerMode),
    showScore: (question, item) =>
      !!item.attribute.examCorrectAnswer &&
      [ExamAnswerMode.select, ExamAnswerMode.selectCorrect].includes(question.attribute.examAnswerMode)
  },
  [QuestionType.FillBlank]: {
    inputType: 'text',
    inputTitle: true,
    modelPath: 'attribute.examCorrectAnswer',
    answerModes: [
      { label: '全部答对才得分', value: 'selectAll' },
      { label: '答对几项得几分', value: 'select' },
      { label: '人工评分', value: 'manual' }
    ],
    matchRules: [
      { label: '回答与答案完全相同得分', value: 'completeSame' },
      { label: '回答中包含答案即可得分', value: 'contain' }
    ],
    showCorrectAnswer: () => false,
    showScore: (question) =>
      [ExamAnswerMode.select, ExamAnswerMode.selectCorrect].includes(question.attribute.examAnswerMode)
  },
  [QuestionType.MultipleBlank]: {
    inputType: 'text',
    inputTitle: true,
    modelPath: 'attribute.examCorrectAnswer',
    answerModes: [
      { label: '全部答对才得分', value: 'selectAll' },
      { label: '答对几项得几分', value: 'select' },
      { label: '人工评分', value: 'manual' }
    ],
    matchRules: [
      { label: '回答与答案完全相同得分', value: 'completeSame' },
      { label: '回答中包含答案即可得分', value: 'contain' }
    ],
    showCorrectAnswer: () => false,
    showScore: (question) =>
      [ExamAnswerMode.select, ExamAnswerMode.selectCorrect].includes(question.attribute.examAnswerMode)
  },
  [QuestionType.Textarea]: {
    inputType: 'textarea',
    inputResize: 'none',
    modelPath: 'attribute.examCorrectAnswer',
    answerModes: [
      { label: 'AI打分', value: 'ai' },
      { label: '人工评分', value: 'manual' }
    ],
    showCorrectAnswer: () => false,
    showScore: () => false
  }
};

// endregion

// region 考试设置配置项

// 字段配置项
export interface SwitchFieldConfig {
  label: string;
  tooltip?: {
    content: string,
    icon?: string
  };
}

export interface ComponentFieldConfig {
  label: string
  tooltip?: any
  component?: Component
  props?: Record<string, any>
  resetValue: any,
  hasValue: any,
}

export const AnswerSettingConfigs: Record<string, SwitchFieldConfig> = {
  autoSave: {
    label: '开启自动暂存',
    tooltip: {
      content: '开启后，可以自动保存本次未提<br />交的填写内容,再次打开项目可<br />显示上次未提交的填写内容'
    }
  },
  questionNumber: {
    label: '显示题目序号',
    tooltip: {
      content: '勾选后，问卷中的题目会按排列顺序<br />从1开始显示题目序号'
    }
  },
  progressBar:{
    label: '显示进度条',
    tooltip: {
      content: '勾选后，填写者滑动页面可以看到当<br/>前问卷填写进度'
    }
  },
  answerSheetVisible: {
    label: '显示答题卡'
  },
  copyEnabled: {
    label: '允许复制题目',
    tooltip: {
      content: '关闭后，问卷内容将不允许被复制。'
    }
  }
};

// 考试设置配置项
export const ExamSettingConfigs: Record<string, ComponentFieldConfig> = {
  randomProject:{
    label: '随机抽取题目',
    resetValue: null,
    hasValue: (v: any) => v !== undefined
  },
  startTime: {
    label: '考试开始时间',
    component: markRaw(ElDatePicker),
    props: { type: 'datetime' },
    resetValue: null,
    hasValue: (v: any) => v !== undefined
  },
  endTime: {
    label: '考试结束时间',
    component: markRaw(ElDatePicker),
    props: { type: 'datetime' },
    resetValue: null,
    hasValue: (v: any) => v !== undefined
  },
  minSubmitMinutes: {
    label: '最短交卷时间',
    component: markRaw(ElInputNumber),
    props: { min: 0, precision: 0 },
    resetValue: null,
    hasValue: (v: number) => v > 0
  },
  maxSubmitMinutes: {
    label: '最长交卷时间',
    tooltip:{
      content: '到达交卷时间后系统将自动提交问卷'
    },
    component: markRaw(ElInputNumber),
    props: { min: 0, precision: 0 },
    resetValue: null,
    hasValue: (v: number) => v > 0
  },
  maxSwitchScreenTimes: {
    label: '最大切屏次数',
    component: markRaw(ElInputNumber),
    props: { min: 0, precision: 0 },
    resetValue: null,
    hasValue: (v: number) => v > 0
  },
  passScore: {
    label: '及格分数',
    component: markRaw(ElInputNumber),
    props: { min: 0, max: 100, precision: 0 },
    resetValue: null,
    hasValue: (v: number) => v > 0
  },
  loginLimit: {
    label: '考试次数限制',
    component: markRaw(ElInputNumber),
    props: { min: 1, max: 10 },
    resetValue: null,
    hasValue: (v: number) => v !== undefined
  }
};

export  const ExamSettingSwitchConfigs: Record<string, SwitchFieldConfig> = {
  exerciseMode: {
    label: '练习模式',
    tooltip: {
      content: '答完每题后立即显示答案。仅支<br />持设有正确答案的单选题、多选<br />题、判断题及填空题。'
    }
  },
  randomOrder: {
    label: '随机选项顺序',
    tooltip: {
      content: '开启之后，选项顺序将被打乱'
    }
  },

}

export const SubmittedSettingConfigs: Record<string, ComponentFieldConfig> = {
  loginLimit:{
    label:'每个IP答题次数限制',
    component: markRaw(ElInputNumber),
    props: { min: 1, max: 10 },
    resetValue: null,
    hasValue: (v: number) => v !== undefined
  }
}
// endregion
